<?php
/*IMPORTANT: 注意检查文件字符集是否为UTF-8*/
class server extends base
{
	public $ipaddrs;
	public $hostname;
	public $lastcheck;
	public $laststat;
	public $sn;
	public $info;
	protected $roleid;
	protected $uuid;
	protected $enabled;
	public function __construct($id=0)
	{
		parent::__construct();
		if($id>0)
		{
			$this->Load($id);
		}
	}
	public function __destruct()
	{
		parent::__destruct();
		unset($this->ipaddrs);
		unset($this->hostname);
		unset($this->lastcheck);
		unset($this->laststat);
		unset($this->sn);
		unset($this->info);
		unset($this->roleid);
		unset($this->uuid);
		unset($this->enabled);
	}
	public function GetList($index=0,$num=10,$hideDisabled=true)
	{
		$db = new db;
		$servers=$db->Get('Server',null,null,false,$index,$num);
		$arrServersList = array();
		foreach ($servers as $server)
		{
			if (@$server['id'])
			{
				$server = new server($server['id']);
				array_push ($arrServersList, $server);
			}
		}
		return $arrServersList;
	}
	public function UpdateInfo($uuid,$info,$status="Miss")
	{
		$db = new db;
		$update=$db->Set('Server',array('uuid'=>$uuid),array('info'=>$info,'laststat'=>$status,'lastcheck'=>date("Y-m-d H:i:s")));
		if ($update)
		{
			return true;
		}
		return false;
	}
	private function Load($id)
	{
		if($id<=0)
		{
			return ;
		}
		$server = $this->LoadConvert($id);
		if($server)
		{
			$this->id = $server['id'];
			$this->ipaddrs = $server['ipaddrs'];
			$this->hostname = $server['hostname'];
			$this->lastcheck = $server['lastcheck'];
			$this->laststat = $server['laststat'];
			$this->sn = $server['sn'];
			$this->info = $server['info'];
			$this->roleid = $server['roleid'];
			$this->uuid = $server['uuid'];
			$this->enabled = $server['enabled'];
		}
	}
	private function LoadConvert($id)
	{
		if($id<=0)
		{
			return ;
		}
		$db = new db;
		$server = $db->GetOne('Server',$id);
		$convertedServer = array(
			'id'=>$server['id'],
			'ipaddrs'=>array(),
			'hostname'=>$server['hostname'],
			'lastcheck'=>$server['lastcheck'],
			'laststat'=>$server['laststat'],
			'sn'=>$server['sn'],
			'info'=>array(),
			'roleid'=>$server['roleid'],
			'uuid'=>$server['uuid'],
			'enabled'=>$server['enabled']
			);
		$convertedServer['ipaddrs'] = $this->ServerIpaddrsSplit($server['ipaddrs']);
		$convertedServer['info'] = $this->ServerInfoSplit($server['info']);
		return $convertedServer;
	}
	private function ServerIpaddrsSplit($ipaddrs)
	{
		$arrIpaddrs = array();
		$token = strtok($ipaddrs,",");
		while (@$token)
		{
			array_push($arrIpaddrs,$token);
			$token = strtok(",");
		}
		return $arrIpaddrs;
	}
	private function ServerInfoSplit($info)
	{
		$arrInfo = array();
		$token = strtok($info,",");
		while (@$token)
		{
			array_push($arrInfo,$token);
			$token = strtok(",");
		}
		return $arrInfo;
	}
}
?>